﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VatTuDAO;
using MySql.Data.MySqlClient;
using VatTuDTO;
using System.Data;

namespace VatTuDAO1
{
    public class ChiTietHoaDonDAO : BaseDataProvider
    {
        MySqlCommand _cmd;
        public Boolean ThemChiTietHoaDon(ChiTietHoaDonDTO ct)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "insert into ChiTietHoaDon(MaHoaDon,MaSanPham,SoLuong,DonGiaBan) values(@MaHoaDon,@MaSanPham,@SoLuong,@DonGiaBan)";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@MaHoaDon", ct.MaHoaDon));
            _cmd.Parameters.Add(new MySqlParameter("@MaSanPham", ct.MaSanPham));
            _cmd.Parameters.Add(new MySqlParameter("@SoLuong", ct.SoLuong));
            _cmd.Parameters.Add(new MySqlParameter("@DonGiaBan", ct.DonGiaBan));


            return ExecuteNonQuery(_cmd);
        }
        public List<ChiTietHoaDonDTO> LayDanhSachChiTietHoaDon()
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from ChiTietHoaDon";
            _cmd.CommandType = CommandType.Text;

            DataTable dt = new DataTable();

            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            {
                return null;
            }

            List<ChiTietHoaDonDTO> list = new List<ChiTietHoaDonDTO>();

            foreach (DataRow row in dt.Rows)
            {
                ChiTietHoaDonDTO dto = new ChiTietHoaDonDTO();
                dto.MaChiTietHoaDon = int.Parse(row["MaChiTietHoaDon"].ToString());
                dto.MaHoaDon = int.Parse(row["MaHoaDon"].ToString());
                dto.MaSanPham = int.Parse(row["MaSanPham"].ToString());
                dto.SoLuong = int.Parse(row["SoLuong"].ToString());
                dto.DonGiaBan = int.Parse(row["DonGiaBan"].ToString());


                list.Add(dto);
            }

            return list;
        }
        public List<ChiTietHoaDonDTO> LayDanhSachChiTietHoaDonTheoMa(int maHoaDon)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from ChiTietHoaDon where MaHoaDon="+maHoaDon;
            _cmd.CommandType = CommandType.Text;

            DataTable dt = new DataTable();

            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            {
                return null;
            }

            List<ChiTietHoaDonDTO> list = new List<ChiTietHoaDonDTO>();

            foreach (DataRow row in dt.Rows)
            {
                ChiTietHoaDonDTO dto = new ChiTietHoaDonDTO();
                dto.MaChiTietHoaDon = int.Parse(row["MaChiTietHoaDon"].ToString());
                dto.MaHoaDon = int.Parse(row["MaHoaDon"].ToString());
                dto.MaSanPham = int.Parse(row["MaSanPham"].ToString());
                dto.SoLuong = int.Parse(row["SoLuong"].ToString());
                dto.DonGiaBan = int.Parse(row["DonGiaBan"].ToString());


                list.Add(dto);
            }

            return list;
        }
        //-------------Quân viết thêm--------------------------------------------------

        public List<ChiTietHoaDonDTO> LayCTHDTheoMaHD(ChiTietHoaDonDTO ct)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from ChiTietHoaDon where MaHoaDon = @MaHoaDon";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@MaChiTietHoaDon", ct.MaHoaDon));

            DataTable dt = new DataTable();

            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            {
                return null;
            }

            List<ChiTietHoaDonDTO> list = new List<ChiTietHoaDonDTO>();

            foreach (DataRow row in dt.Rows)
            {
                ChiTietHoaDonDTO dto = new ChiTietHoaDonDTO();
                dto.MaChiTietHoaDon = int.Parse(row["MaChiTietHoaDon"].ToString());
                dto.MaHoaDon = int.Parse(row["MaHoaDon"].ToString());
                dto.MaSanPham = int.Parse(row["MaSanPham"].ToString());
                dto.SoLuong = int.Parse(row["SoLuong"].ToString());
                dto.DonGiaBan = int.Parse(row["DonGiaBan"].ToString());


                list.Add(dto);
            }

            return list;
        }

        //-------------------------------------------------------------------------------

        public ChiTietHoaDonDTO LayChiTietHoaDon(int MaChiTietHoaDon)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from ChiTietHoaDon where MaCTHD = @MaCTHD";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@MaChiTietHoaDon", MaChiTietHoaDon.ToString()));


            DataTable dt = new DataTable();
            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            {
                return null;
            }

            ChiTietHoaDonDTO dto = new ChiTietHoaDonDTO();

            try
            {

                DataRow row = dt.Rows[0];//kết quả trả ra ở dòng 0

                dto.MaChiTietHoaDon = int.Parse(row["MaChiTietHoaDon"].ToString());
                dto.MaHoaDon = int.Parse(row["MaHoaDon"].ToString());
                dto.MaSanPham = int.Parse(row["MaSanPham"].ToString());
                dto.SoLuong = int.Parse(row["SoLuong"].ToString());
                dto.DonGiaBan = int.Parse(row["DonGiaBan"].ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dto;
        }

        public Boolean CapNhatChiTietHoaDon(ChiTietHoaDonDTO ct)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "update ChiTietHoaDon set SoLuong = @SoLuong , DonGia = @DonGia , MaHD = @MaHD , MaSP = @MaSP where MaCTHD = @MaCTHD";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@MaHoaDon", ct.MaHoaDon));
            _cmd.Parameters.Add(new MySqlParameter("@MaSanPham", ct.MaSanPham));
            _cmd.Parameters.Add(new MySqlParameter("@SoLuong", ct.SoLuong));
            _cmd.Parameters.Add(new MySqlParameter("@DonGiaBan", ct.DonGiaBan));

            _cmd.Parameters.Add(new MySqlParameter("@MaChiTietHoaDon", ct.MaChiTietHoaDon));

            return ExecuteNonQuery(_cmd);
        }

        public Boolean XoaChiTietHoaDon(int MaChiTietHoaDon)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "delete from ChiTietHoaDon where MaChiTietHoaDon = @MaChiTietHoaDon";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@MaChiTietHoaDon", MaChiTietHoaDon));

            return ExecuteNonQuery(_cmd);
        }


        public Boolean XoaChiTietHoaDon_MaHD(int MaHoaDon)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "delete from ChiTietHoaDon where MaHoaDon = @MaHoaDon";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@MaHoaDon", MaHoaDon));

            return ExecuteNonQuery(_cmd);
        }
       
    }
}
